/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Bankaccount;
import domain.Expense;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alejandro
 */
public class ExpenseDao {

    EntityManager em;
    EntityTransaction tx;

    public ExpenseDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Expense getExpense(Integer idExpense){
        Expense i = (Expense) em.createNativeQuery("select * from expense as i where i.idExpense='"+idExpense+"'", Expense.class).getSingleResult();
        return i;
    }

    public void removeExpense(Expense expense) {
        tx.begin();
        em.remove(expense);
        tx.commit();
    }

    public void saveExpense(Expense i){
        tx.begin();
        em.persist(i);
        tx.commit();
    }

    public Set<Expense> getExpenseList(Bankaccount b){

        Set<Expense> s = new HashSet(em.createNativeQuery("select * from expense as i where i.userFK='"+b.getIdBank()+"'", Expense.class).getResultList());
        return s;
    }




}
